//
//  MoyaProviderFactory.swift
//  YD_DataModel
//
//  Created by 钟志远 on 2021/11/30.
//  Copyright © 2021 alyadan. All rights reserved.
//

import Foundation
import Moya

open class DefaultMoyaProviderFactory: MoyaProviderFactoryType {
    
    let timeout: TimeInterval
    
    init(timeout: TimeInterval = 10) {
        self.timeout = timeout
    }
    
   open func requestTimeoutClosure<T: TargetType>(target: T) -> MoyaProvider<T>.RequestClosure {
        let requestTimeoutClosure = { (endPoint: Endpoint, done: @escaping MoyaProvider<T>.RequestResultClosure) in
            do {
                var request = try endPoint.urlRequest()
                request.timeoutInterval = self.timeout
                done(.success(request))
            } catch {
                return
            }
        }
        return requestTimeoutClosure
    }
    
    public func makeProvider<T>(target: T) -> MoyaProvider<T> where T : TargetType {
        return MoyaProvider.init(requestClosure: self.requestTimeoutClosure(target: target))
    }
}
